#!/usr/bin/python
# -*- coding: UTF-8 -*-
from io import StringIO

import pymongo;
from config import *
import pandas as pd
from datetime import datetime
from music.DataFilter import FilterData
import matplotlib.pyplot as plot
from utils.GwordClude import generate_cloud_word
import jieba
import re
#指定默认字体
plot.rcParams['font.sans-serif'] = ['SimHei']
plot.rcParams['font.family']='sans-serif'
# https://blog.csdn.net/zenghaihong/article/details/70747247 时间

client=pymongo.MongoClient(MONGO_URL);
db=client["163music"];


def get_data():
    queryArgs = {}
    projectionFields = {'_id': False,"user_url":True,"zan_count":True,"user_comment":True}  # 用字典指定
    result = db["fengjuzhu"].find(queryArgs,projectionFields)
    return result


def get_music_commnet(data):
    result=list(data["user_comment"])
    string=StringIO()
    for word in result:
        string.write(str(word))
    return string.getvalue()


def main():

    # data=pd.DataFrame(list(get_data()))
    show_zan()










    # plot.hist(data["timestamps"])
    # plot.show()


def genge_clude(data):
    all_words = get_music_commnet(data)
    generate_cloud_word(jieba.cut(all_words), "../utils/aaaa.jpg", "music_com.jpg")


def show_zan():
    labels = '<10', '(10,50)', '(50,200)', '>200'
    fracs = [12.8, 3.6, 5.5, 78.1]
    explode = (0, 0.05, 0, 0)
    # Make square figures and axes
    plot.axes(aspect=1)
    explode = (0, 0.05, 0, 0)
    plot.title("评论点赞数分布情况")
    # plt.pie(fracs, labels=labels, autopct='%1.1f%%', shadow=True)
    plot.pie(fracs, explode=explode, labels=labels, autopct='%.0f%%', shadow=True)
    plot.legend(['小于10', '10到50', '50到200','200以上'],loc="upper left",bbox_to_anchor=(1,1))
    plot.savefig("zan.png")
    plot.show()


def zan(data):
    print(data.describe())
    data = FilterData(data).filter_zan()
    data = FilterData(data).group_zan()
    data = data.groupby(by="type").sum()
    data.to_csv("data.csv", encoding="utf-8")
    return data


def cm_date(data):
    data = FilterData(data).filter_time()
    print(data.head())
    data = data.reindex(index=data["timestamps"], columns=["user_name"])
    data = data.fillna(1)
    data = data.resample("M", how="sum")
    plot.xlabel("评论时间")
    plot.ylabel("评论次数")
    plot.title('歌曲"风居住过的街道"评论趋势')
    plot.plot(data.index, data["user_name"])
    plot.savefig("wind.png")
    plot.show()


def strTime(str):
    time =""
    if "年" in str:
        time_mode = "%Y年%m月%d日 %H:%M"
        time=datetime.strptime(str, time_mode)
    else:
        time_mode = "%m月%d日 %H:%M"
        time = datetime.strptime(str, time_mode)
        print(time.year)
        time_now=datetime.now()
        time=datetime(time_now.year,time.month,time.day,time.hour,time.minute)
    return time

if __name__ == '__main__':
   # print(strTime("2015年12月3日 3:40"))
    main()
